package com.googlecode.jtoy.patterns.mediator;

/**
 * 中介者模式
 * 
 * 一个是主播 Anchor 一个是嘉宾 Guest 
 * 主播说话的时候，嘉宾要停下，嘉宾只能在主播不说话的时候说话。
 * 
 * @author shangxj
 * @date Jun 19, 2011 6:28:54 PM
 */
public class Mediator {

	private Anchor anchor;
	private Guest guest;

	public void regAnchor(Anchor anchor) {

		this.anchor = anchor;
	}

	public void regGuest(Guest guest) {

		this.guest = guest;
	}

	public void anchorSpeak() {

		if (!guest.isFree()) {
			guest.stop();
		}
		System.out.println("Anchor is speaking....");
		anchor.setFree(false);
	}

	public void guestSpeak() {

		if (anchor.isFree()) {
			System.out.println("Guest is speaking....");
			guest.setFree(false);
		}
		else {
			System.out.println("Anchor is speaking. Do not interrupt...");
		}

	}

	public void anchorStop() {

		System.out.println("Anchor stop speaking now....");
		anchor.setFree(true);
	}

	public void guestStop() {

		System.out.println("Guest stop speaking now...");
		guest.setFree(true);

	}
}
